/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2021-03-19 20:46:10
 * @LastEditors: sueRimn
 * @LastEditTime: 2021-03-23 19:49:39
 */
//!从构建工具中导入的方法
const { src, dest, watch, series } = require('gulp');

//!引入的插件
const htmlClean = require('gulp-htmlclean');
const less = require('gulp-less');
const cleanCss = require('gulp-clean-css');
const stripDebug = require('gulp-strip-debug');
const uglify = require('gulp-uglify');
const imageMin = require('gulp-imagemin');
const connect = require('gulp-connect');
const imagemin = require('gulp-imagemin');

//!方便目录的统一更改
const folder = {
    src: 'src/',
    dist: 'dist/',
}

//!需要执行的任务
function html () {
    return src(folder.src + 'html/*')
        .pipe(htmlClean())                      //压缩html代码
        .pipe(dest(folder.dist))      //输出文件
        .pipe(connect.reload());                //重新加载
}

function css () {
    return src(folder.src + 'css/*')
        .pipe(less())                           //将less文件转为css文件
        .pipe(cleanCss())                       //压缩css文件
        .pipe(dest(folder.dist + 'css/'))       //输出css文件
        .pipe(connect.reload());                //重新加载
}

function js () {
    return src(folder.src + 'js/*')
        .pipe(stripDebug())                     //去除调试语句
        .pipe(uglify())                         //压缩代码
        .pipe(dest(folder.dist + 'js/'))        //输出js文件
        .pipe(connect.reload());                //重新加载
}

function image () {
    return src(folder.src + 'images/*')
        .pipe(imagemin())                       //压缩图片
        .pipe(dest(folder.dist + 'images/'));          //输出图片文件
}

function server (cb) {
    connect.server({
        port: '1000',                           //端口号
        livereload: true,                       //自动刷新    
    });
    cb();
}


//!文件监控
watch(folder.src + 'html/*', function (cb) {
    html();
    cb();
});
watch(folder.src + 'css/*', function (cb) {
    css();
    cb();
});
watch(folder.src + 'js/*', function (cb) {
    js();
    cb();
});


//!
exports.default = series(html, css, js, image, server);